<?php
class BlockData {
	public $blockTable;
	public $productTable;
	public $subProductTable;
	public $storeTables=array();
	
	public  function __construct($db){
		$this->blockTable = new BaseDao($db, 'cms_block','block_id');
		$this->storeTables[1]  = new BaseDao($db, 'cms_store_link','id');
		$this->storeTables[2]  = new BaseDao($db, 'cms_store_img','id');
		$this->storeTables[3]  = new BaseDao($db, 'cms_store_product','id');
		$this->productTable = new BaseDao($db,'goods_product','pro_id');
		$this->subProductTable = new BaseDao($db,'goods_sub_product','spro_id');
	}
	
	public function assignBlocks($channelid,&$smarty){
		$blocks = $this->blockTable->getAll('channel_id=?', array($channelid));
		foreach ($blocks as $block){
			$storeTable = $this->storeTables[$block['block_type']];
			$stores = $storeTable->getAll('block_id=?', array($block['block_id']));
			if($block['block_type'] == 3){
				foreach ($stores as $key=>$store){
					$sp = $this->subProductTable->getOne("spro_id=? and is_delete=?", array($store['spro_id'],0));
					if(isset($sp) && count($sp)>0){
						$p = $this->productTable->getOne("pro_id=? and is_delete=?", array($sp['pro_id'],0));
						$sp['product'] = $p;
					}
					$stores[$key]['subproduct'] = $sp;
				}
			}
			$smarty->assign($block['block_sn'],$stores);
		}
	}
}
?>